package cn.edu.jxust.controller.api.enterprise;

import cn.edu.jxust.common.domain.entity.Account;
import cn.edu.jxust.common.domain.entity.Enterprise;
import cn.edu.jxust.common.domain.valobj.EnterpriseVo;
import cn.edu.jxust.common.domain.web.api.enterprise.IEnterpriseController;
import cn.edu.jxust.common.response.Result;
import cn.edu.jxust.common.response.ResultUtils;
import cn.edu.jxust.common.response.enums.ResultStatus;
import cn.edu.jxust.core.domain.repository.EnterpriseService;
import cn.edu.jxust.core.infrastructure.util.Const;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author QiuKangming
 * @since 2020-06-29
 * @description 企业信息管理接口
 *  权限说明:
 *      超级管理员: 对所有区域的企业信息可以操作
 *      局机管理员: 和超级管理员一样
 *      区级管理员: 只能对所管辖的区域的企业可操作
 *      企业用户没有权限操作
 */

@Slf4j
@RestController
public class EnterpriseController implements IEnterpriseController {

    @Resource
    private EnterpriseService enterpriseService;

    @Override
    public Result addEnterprise(EnterpriseVo enterpriseVo, BindingResult result){

        if(result.hasErrors() || enterpriseVo.getCreateUserid() == null){
            return ResultUtils.resultWithoutData(ResultStatus.PARAM_ERROR);
        }

        try{
            return enterpriseService.addEnterprise(enterpriseVo);
        }catch (Exception e){
            log.error("EnterpriseController --> addEnterprise 异常: {}", e.getMessage());
            return ResultUtils.resultWithoutData(ResultStatus.FAILED);
        }

    }

    @Override
    public Result delEnterpriseById(Integer id){

        try{
            return enterpriseService.delEnterpriseById(id);
        }catch (Exception e){
            log.error("EnterpriseController --> delEnterpriseById 异常: {}", e.getMessage());
            return ResultUtils.resultWithoutData(ResultStatus.FAILED);
        }

    }

    @Override
    public Result modifyEnterprise(EnterpriseVo enterpriseVo, BindingResult result){

        if(result.hasErrors() || enterpriseVo.getId() == null){
            return ResultUtils.resultWithoutData(ResultStatus.PARAM_ERROR);
        }
        try{
            return enterpriseService.modifyEnterpriseInfo(enterpriseVo);
        }catch (Exception e){
            log.error("EnterpriseController --> modifyEnterprise 异常: {}", e.getMessage());
            return ResultUtils.resultWithoutData(ResultStatus.FAILED);
        }

    }

    @Override
    public Result listEnterpriseOrQuality(Integer page, Integer size, HttpServletRequest request){

        try{
            //获取当前用户
            Account account = (Account) SecurityUtils.getSubject().getPrincipal();
            if(account.getRoleId().equals(Const.ENTERPRISE_USER_ID)){
                return ResultUtils.resultWithoutData(ResultStatus.VISITED_FORBID);
            }
            //获取客户端传过来的数组, 需要排除 page 和 size
            Map<String, String[]> paramMap = request.getParameterMap();
            IPage<EnterpriseVo> enterpriseVoList = enterpriseService.getEnterpriseVoList(paramMap, account, page, size);
            if(enterpriseVoList.getRecords() != null && !enterpriseVoList.getRecords().isEmpty()){
                return ResultUtils.resultWithData(ResultStatus.SUCCESS, enterpriseVoList);
            }else {
                return ResultUtils.resultWithoutData(ResultStatus.NOT_FOUND, "未找到任何企业相关信息");
            }
        }catch (Exception e){
            log.error("EnterpriseController --> listEnterpriseOrQuality 异常: {}", e.getMessage());
            return ResultUtils.resultWithoutData(ResultStatus.FAILED);
        }

    }

    @Override
    public Result getEnterpriseListByAreaId(Integer areaId){

        try{
            List<Enterprise> list = enterpriseService.getEnterpriseListByAreaId(areaId);
            if(null != list && list.size() != 0){
                return ResultUtils.resultWithData(ResultStatus.SUCCESS, list);
            }else{
                return ResultUtils.resultWithoutData(ResultStatus.NOT_FOUND, "未找到任何有关企业信息");
            }
        }catch (Exception e){
            log.error("[EnterpriseController --> getEnterpriseListByAreaId 异常]: {}", e.getMessage());
            return ResultUtils.resultWithoutData(ResultStatus.FAILED);
        }

    }
}